  CC = gcc 

  
  128SL_CFLAGS = -DCATEGORY=1
  192SL_CFLAGS = -DCATEGORY=3
  256SL_CFLAGS = -DCATEGORY=5
  
  N02_CFLAGS = -DN0=2
  N03_CFLAGS = -DN0=3
  N04_CFLAGS = -DN0=4

  REFERENCE_IMPL_PATH = ../../../Reference_Implementation/KEM
  
  CFLAGS = -DCPU_WORD_BITS=64 -std=c99 -Wall -pedantic -Wmaybe-uninitialized -Wuninitialized -march=nocona -funroll-loops -O3
  LDFLAGS = -lm
  INCLUDES = -I./include -I$(REFERENCE_IMPL_PATH)/include
  SRCDIR_MAIN_FILE = library
  SRCDIR_LIB_FILE  = $(REFERENCE_IMPL_PATH)/library

# Gathers the names of all C files 
  CSRC_LIB_FILE =  $(wildcard $(SRCDIR_LIB_FILE)/*.c) 
  CSRC_MAIN_FILE = $(wildcard $(SRCDIR_MAIN_FILE)/*.c) 

  BINDIR = bin/
  
  BINDIR_128SL_N02  = $(BINDIR)128SL_N02
  BINDIR_128SL_N03  = $(BINDIR)128SL_N03
  BINDIR_128SL_N04  = $(BINDIR)128SL_N04
  
  BINDIR_192SL_N02 = $(BINDIR)192SL_N02
  BINDIR_192SL_N03 = $(BINDIR)192SL_N03
  BINDIR_192SL_N04 = $(BINDIR)192SL_N04
  
  BINDIR_256SL_N02 = $(BINDIR)256SL_N02
  BINDIR_256SL_N03 = $(BINDIR)256SL_N03
  BINDIR_256SL_N04 = $(BINDIR)256SL_N04
  

  ALL_BINARIES = $(BINDIR_128SL_N02)/test_128SL_N02 \
                 $(BINDIR_128SL_N03)/test_128SL_N03 \
                 $(BINDIR_128SL_N04)/test_128SL_N04 \
                 $(BINDIR_192SL_N02)/test_192SL_N02 \
                 $(BINDIR_192SL_N03)/test_192SL_N03 \
                 $(BINDIR_192SL_N04)/test_192SL_N04 \
                 $(BINDIR_256SL_N02)/test_256SL_N02 \
                 $(BINDIR_256SL_N03)/test_256SL_N03 \
                 $(BINDIR_256SL_N04)/test_256SL_N04

.PHONY : all
all: $(BINDIR_128SL_N02) $(BINDIR_128SL_N03) $(BINDIR_128SL_N04) \
$(BINDIR_192SL_N02) $(BINDIR_192SL_N03) $(BINDIR_192SL_N04) \
$(BINDIR_256SL_N02) $(BINDIR_256SL_N03) $(BINDIR_256SL_N04) \
$(ALL_BINARIES)


# Beginning of Security Level 128 
$(BINDIR_128SL_N02) $(BINDIR_128SL_N03) $(BINDIR_128SL_N04):
	mkdir -p $@
	
COBJS_BINDIR_128SL_N02=$(CSRC_LIB_FILE:$(SRCDIR_LIB_FILE)/%.c=$(BINDIR_128SL_N02)/%.o)
COBJS_BINDIR_128SL_N03=$(CSRC_LIB_FILE:$(SRCDIR_LIB_FILE)/%.c=$(BINDIR_128SL_N03)/%.o)
COBJS_BINDIR_128SL_N04=$(CSRC_LIB_FILE:$(SRCDIR_LIB_FILE)/%.c=$(BINDIR_128SL_N04)/%.o)

COBJS_BINDIR_128SL_N02 += $(CSRC_MAIN_FILE:$(SRCDIR_MAIN_FILE)/%.c=$(BINDIR_128SL_N02)/%.o)
COBJS_BINDIR_128SL_N03 += $(CSRC_MAIN_FILE:$(SRCDIR_MAIN_FILE)/%.c=$(BINDIR_128SL_N03)/%.o)
COBJS_BINDIR_128SL_N04 += $(CSRC_MAIN_FILE:$(SRCDIR_MAIN_FILE)/%.c=$(BINDIR_128SL_N04)/%.o)

$(BINDIR_128SL_N02)/%.o: $(SRCDIR_MAIN_FILE)/%.c 
	$(CC) $(CFLAGS) $(128SL_CFLAGS) $(N02_CFLAGS) $(INCLUDES) -c $< -o $@
$(BINDIR_128SL_N03)/%.o: $(SRCDIR_MAIN_FILE)/%.c
	$(CC) $(CFLAGS) $(128SL_CFLAGS) $(N03_CFLAGS) $(INCLUDES) -c $< -o $@
$(BINDIR_128SL_N04)/%.o: $(SRCDIR_MAIN_FILE)/%.c
	$(CC) $(CFLAGS) $(128SL_CFLAGS) $(N04_CFLAGS) $(INCLUDES) -c $< -o $@

$(BINDIR_128SL_N02)/%.o: $(SRCDIR_LIB_FILE)/%.c
	$(CC) $(CFLAGS) $(128SL_CFLAGS) $(N02_CFLAGS) $(INCLUDES) -c $< -o $@
$(BINDIR_128SL_N03)/%.o: $(SRCDIR_LIB_FILE)/%.c 
	$(CC) $(CFLAGS) $(128SL_CFLAGS) $(N03_CFLAGS) $(INCLUDES) -c $< -o $@
$(BINDIR_128SL_N04)/%.o: $(SRCDIR_LIB_FILE)/%.c
	$(CC) $(CFLAGS) $(128SL_CFLAGS) $(N04_CFLAGS) $(INCLUDES) -c $< -o $@
	

	
$(BINDIR_128SL_N02)/test_128SL_N02: $(COBJS_BINDIR_128SL_N02)
	$(CC) $(COBJS_BINDIR_128SL_N02) $(LDFLAGS) -o $(BINDIR_128SL_N02)/test_128SL_N02
$(BINDIR_128SL_N03)/test_128SL_N03: $(COBJS_BINDIR_128SL_N03)
	$(CC) $(COBJS_BINDIR_128SL_N03) $(LDFLAGS) -o $(BINDIR_128SL_N03)/test_128SL_N03
$(BINDIR_128SL_N04)/test_128SL_N04: $(COBJS_BINDIR_128SL_N04)
	$(CC) $(COBJS_BINDIR_128SL_N04) $(LDFLAGS) -o $(BINDIR_128SL_N04)/test_128SL_N04

# ######## Security Level 192

$(BINDIR_192SL_N02) $(BINDIR_192SL_N03) $(BINDIR_192SL_N04):
	mkdir -p $@
	
COBJS_BINDIR_192SL_N02=$(CSRC_LIB_FILE:$(SRCDIR_LIB_FILE)/%.c=$(BINDIR_192SL_N02)/%.o)
COBJS_BINDIR_192SL_N03=$(CSRC_LIB_FILE:$(SRCDIR_LIB_FILE)/%.c=$(BINDIR_192SL_N03)/%.o)
COBJS_BINDIR_192SL_N04=$(CSRC_LIB_FILE:$(SRCDIR_LIB_FILE)/%.c=$(BINDIR_192SL_N04)/%.o)

COBJS_BINDIR_192SL_N02 += $(CSRC_MAIN_FILE:$(SRCDIR_MAIN_FILE)/%.c=$(BINDIR_192SL_N02)/%.o)
COBJS_BINDIR_192SL_N03 += $(CSRC_MAIN_FILE:$(SRCDIR_MAIN_FILE)/%.c=$(BINDIR_192SL_N03)/%.o)
COBJS_BINDIR_192SL_N04 += $(CSRC_MAIN_FILE:$(SRCDIR_MAIN_FILE)/%.c=$(BINDIR_192SL_N04)/%.o)

$(BINDIR_192SL_N02)/%.o: $(SRCDIR_MAIN_FILE)/%.c 
	$(CC) $(CFLAGS) $(192SL_CFLAGS) $(N02_CFLAGS) $(INCLUDES) -c $< -o $@
$(BINDIR_192SL_N03)/%.o: $(SRCDIR_MAIN_FILE)/%.c
	$(CC) $(CFLAGS) $(192SL_CFLAGS) $(N03_CFLAGS) $(INCLUDES) -c $< -o $@
$(BINDIR_192SL_N04)/%.o: $(SRCDIR_MAIN_FILE)/%.c
	$(CC) $(CFLAGS) $(192SL_CFLAGS) $(N04_CFLAGS) $(INCLUDES) -c $< -o $@

$(BINDIR_192SL_N02)/%.o: $(SRCDIR_LIB_FILE)/%.c
	$(CC) $(CFLAGS) $(192SL_CFLAGS) $(N02_CFLAGS) $(INCLUDES) -c $< -o $@
$(BINDIR_192SL_N03)/%.o: $(SRCDIR_LIB_FILE)/%.c 
	$(CC) $(CFLAGS) $(192SL_CFLAGS) $(N03_CFLAGS) $(INCLUDES) -c $< -o $@
$(BINDIR_192SL_N04)/%.o: $(SRCDIR_LIB_FILE)/%.c
	$(CC) $(CFLAGS) $(192SL_CFLAGS) $(N04_CFLAGS) $(INCLUDES) -c $< -o $@
	

	
$(BINDIR_192SL_N02)/test_192SL_N02: $(COBJS_BINDIR_192SL_N02)
	$(CC) $(COBJS_BINDIR_192SL_N02) $(LDFLAGS) -o $(BINDIR_192SL_N02)/test_192SL_N02
$(BINDIR_192SL_N03)/test_192SL_N03: $(COBJS_BINDIR_192SL_N03)
	$(CC) $(COBJS_BINDIR_192SL_N03) $(LDFLAGS) -o $(BINDIR_192SL_N03)/test_192SL_N03
$(BINDIR_192SL_N04)/test_192SL_N04: $(COBJS_BINDIR_192SL_N04)
	$(CC) $(COBJS_BINDIR_192SL_N04) $(LDFLAGS) -o $(BINDIR_192SL_N04)/test_192SL_N04


# 
# ######## Security Level 256
# 
$(BINDIR_256SL_N02) $(BINDIR_256SL_N03) $(BINDIR_256SL_N04):
	mkdir -p $@
	
COBJS_BINDIR_256SL_N02=$(CSRC_LIB_FILE:$(SRCDIR_LIB_FILE)/%.c=$(BINDIR_256SL_N02)/%.o)
COBJS_BINDIR_256SL_N03=$(CSRC_LIB_FILE:$(SRCDIR_LIB_FILE)/%.c=$(BINDIR_256SL_N03)/%.o)
COBJS_BINDIR_256SL_N04=$(CSRC_LIB_FILE:$(SRCDIR_LIB_FILE)/%.c=$(BINDIR_256SL_N04)/%.o)

COBJS_BINDIR_256SL_N02 += $(CSRC_MAIN_FILE:$(SRCDIR_MAIN_FILE)/%.c=$(BINDIR_256SL_N02)/%.o)
COBJS_BINDIR_256SL_N03 += $(CSRC_MAIN_FILE:$(SRCDIR_MAIN_FILE)/%.c=$(BINDIR_256SL_N03)/%.o)
COBJS_BINDIR_256SL_N04 += $(CSRC_MAIN_FILE:$(SRCDIR_MAIN_FILE)/%.c=$(BINDIR_256SL_N04)/%.o)

$(BINDIR_256SL_N02)/%.o: $(SRCDIR_MAIN_FILE)/%.c 
	$(CC) $(CFLAGS) $(256SL_CFLAGS) $(N02_CFLAGS) $(INCLUDES) -c $< -o $@
$(BINDIR_256SL_N03)/%.o: $(SRCDIR_MAIN_FILE)/%.c
	$(CC) $(CFLAGS) $(256SL_CFLAGS) $(N03_CFLAGS) $(INCLUDES) -c $< -o $@
$(BINDIR_256SL_N04)/%.o: $(SRCDIR_MAIN_FILE)/%.c
	$(CC) $(CFLAGS) $(256SL_CFLAGS) $(N04_CFLAGS) $(INCLUDES) -c $< -o $@

$(BINDIR_256SL_N02)/%.o: $(SRCDIR_LIB_FILE)/%.c
	$(CC) $(CFLAGS) $(256SL_CFLAGS) $(N02_CFLAGS) $(INCLUDES) -c $< -o $@
$(BINDIR_256SL_N03)/%.o: $(SRCDIR_LIB_FILE)/%.c 
	$(CC) $(CFLAGS) $(256SL_CFLAGS) $(N03_CFLAGS) $(INCLUDES) -c $< -o $@
$(BINDIR_256SL_N04)/%.o: $(SRCDIR_LIB_FILE)/%.c
	$(CC) $(CFLAGS) $(256SL_CFLAGS) $(N04_CFLAGS) $(INCLUDES) -c $< -o $@
	

	
$(BINDIR_256SL_N02)/test_256SL_N02: $(COBJS_BINDIR_256SL_N02)
	$(CC) $(COBJS_BINDIR_256SL_N02) $(LDFLAGS) -o $(BINDIR_256SL_N02)/test_256SL_N02
$(BINDIR_256SL_N03)/test_256SL_N03: $(COBJS_BINDIR_256SL_N03)
	$(CC) $(COBJS_BINDIR_256SL_N03) $(LDFLAGS) -o $(BINDIR_256SL_N03)/test_256SL_N03
$(BINDIR_256SL_N04)/test_256SL_N04: $(COBJS_BINDIR_256SL_N04)
	$(CC) $(COBJS_BINDIR_256SL_N04) $(LDFLAGS) -o $(BINDIR_256SL_N04)/test_256SL_N04
	
.PHONY : clean
clean:
	rm -rf $(BINDIR)*
